<!DOCTYPE html>
<html>
<meta charset="utf-8">
<style>

body {
  font: 10px sans-serif;
}

.chord path {
  fill-opacity: .67;
  stroke: #fff;
  stroke-width: .25px;
}

</style>
<body>
<script src="../d3.v3.min.js"></script>
<script src="data/matrix.js"></script>
<script>

// From http://mkweb.bcgsc.ca/circos/guide/tables/

var chord = d3.layout.chord()
    .padding(.05)
    .sortSubgroups(d3.descending)
    .sortChords(d3.ascending)
    .matrix(matrixLeaf);
    
var chordAncestor = d3.layout.chord()
    .padding(.05)
    .sortSubgroups(d3.descending)
    .matrix(matrixAncestor);

var chordParent = d3.layout.chord()
    .padding(.05)
    .sortSubgroups(d3.descending)
    .matrix(matrixParent);
    
var winWidth = 1024,
	winHeight = 750;

var width = winWidth,
    height = winHeight,
    innerRadius = Math.min(width, height) * .41 -40,
    outerRadius = innerRadius * 1.1;

var fill = d3.scale.ordinal()
    .domain(d3.range(4))
    .range(["#00DD00", "#FF0089", "#0044FF", "#F26223"]);

var svg = d3.select("body").append("svg")//这里确定绘图区域的位置
    .attr("width", winWidth)
    .attr("height", winHeight)
  .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

svg.append("g").selectAll("path")//这里画的是圆环上的扇形
    .data(chord.groups)
  .enter().append("path")
    .style("fill", function(d) { return fill(d.index<29?10:30); })
    .style("stroke", function(d) { return fill(d.index<29?10:30); })
    .attr("d", d3.svg.arc().innerRadius(innerRadius).outerRadius(outerRadius))
    .on("mouseover", fade(.1))
    .on("mouseout", fade(1));
    
    svg.append("g").selectAll("path")//这里画的是圆环上的扇形
    .data(chordParent.groups)
	.enter().append("path")
    .style("fill", function(d) { return fill(d.index); })
    .style("stroke", function(d) { return fill(d.index); })
    .style("opacity", .21)
    .attr("d", d3.svg.arc().innerRadius(innerRadius+30).outerRadius(outerRadius+20));
    
    svg.append("g").selectAll("path")//这里画的是圆环上的扇形
    .data(chordAncestor.groups)
	.enter().append("path")
    .style("fill", function(d) { return fill(d.index); })
    .style("stroke", function(d) { return fill(d.index); })
    .style("opacity", .21)
    .attr("d", d3.svg.arc().innerRadius(innerRadius+50).outerRadius(outerRadius+40));

var ticks = svg.append("g").selectAll("g")//这里用来画label前面的细线
    .data(chord.groups)
  .enter().append("g").selectAll("g")
    .data(groupTicks)
  .enter().append("g")
    .attr("transform", function(d) {
      return "rotate(" + (d.angle * 180 / Math.PI - 90) + ")"
          + "translate(" + outerRadius + ",0)";
    });

ticks.append("line")//控制label前面的细线的参数
    .attr("x1", 1)
    .attr("y1", 0)
    .attr("x2", 5)
    .attr("y2", 0)
    .style("stroke", "#000");

ticks.append("text")//控制label的字体和位置等
    .attr("x", 8)
    .attr("dy", ".35em")
    .attr("transform", function(d) { return d.angle > Math.PI ? "rotate(180)translate(-16)" : null; })
    .style("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; })
    .text(function(d) { return d.label; });

svg.append("g")//绘制圆中间的线
    .attr("class", "chord")
  .selectAll("path")
    .data(chord.chords)
  .enter().append("path")
    .attr("d", d3.svg.chord().radius(innerRadius))
    .style("fill", function(d) { return fill(d.target.index<29?30:10); })
    .style("opacity", 1);

// Returns an array of tick angles and labels, given a group.
function groupTicks(d) {
  var k = (d.endAngle - d.startAngle) / d.value;
  return d3.range(0, d.value, 1000).map(function(v, i) {
    return {
      angle: v * k + d.startAngle,
      label: i % 5 ? null : (labelList[d.index].title)
    };
  });
}

// Returns an event handler for fading a given chord group.
function fade(opacity) {
  return function(g, i) {
    svg.selectAll(".chord path")
        .filter(function(d) { return d.source.index != i && d.target.index != i; })
		.transition()
		.attr("stroke-width", 2)
        .style("opacity", opacity);
  };
}

</script>
<script type="text/javascript">
        var xmlHttp;
        //创建XMLhttpRequest对象
        function createXMLHttpRequest() {
            if (window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
            }
        }

        //生成传递到服务器的参数查询串，参数值由页面表单填写得到
       function createQueryString() {
            var firstName = "firstName";
            var middleName = "middleName";
            var birthday = "birthday";
            var queryString = "firstName=" + firstName + "&middleName=" + middleName
            + "&birthday=" + birthday;
            return queryString;
        }

        //点击按钮响应的Get方法主函数
        //Get方法把参数值以名=值方式在xmlHttp.open("GET", queryString, true)中传递,queryString的形式为URL?参数名=值&参数名=值...;而xmlHttp.send(null);
        function doRequestUsingGET() {
            createXMLHttpRequest();//第一步：创建XMLHttpRequest对象
            var queryString = "../../GetAndPostExample?";
            queryString = queryString + createQueryString()
            + "&timeStamp=" + new Date().getTime();//第二步：定义传递的参数值字符串
            xmlHttp.open("GET", queryString, true);//第三步：建立与服务器的请求
            xmlHttp.onreadystatechange = handleStateChange;//第四步：监听状态-->转到监听状态函数    
            xmlHttp.send(null);//第五步：发送请求，并且立即返回
        }

        //点击按钮响应的POST方法主函数
        //POST方法把参数值以名=值方式在xmlHttp.send(queryString)中传递,queryString的形式为参数名=值&参数名=值...;
        function doRequestUsingPOST() {
            createXMLHttpRequest();//第一步：创建XMLHttpRequest对象
            var url = "../../GetAndPostExample?timeStamp=" + new Date().getTime();
            var queryString = createQueryString();//第二步：定义传递的参数值字符串
            xmlHttp.open("POST", url, true);//第三步：建立与服务器的请求
            xmlHttp.onreadystatechange = handleStateChange;//第四步：监听状态-->转到监听状态函数
            xmlHttp.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded;");
            xmlHttp.send(queryString);//第五步：发送请求，并且立即返回
        }

        //监听状态函数
        function handleStateChange() {
            if(xmlHttp.readyState == 4) {
                if(xmlHttp.status == 200) {
                parseResults();//-->转到函数parseResults输出从服务器返的值
                }
            }
        }

        //在页面显示从服务器传来的结果
       function parseResults() {
            var responseDiv = document.getElementById("serverResponse");
            if(responseDiv.hasChildNodes()) {
            responseDiv.removeChild(responseDiv.childNodes[0]);
        }
        var responseText = document.createTextNode(xmlHttp.responseText);//
        responseDiv.appendChild(responseText);
        }
</script>

	<body>
		<form action="#">
			<input type="button" value="Send parameters using GET"
				onclick="doRequestUsingGET();" />
			<!--调用Get方法主函数-->
			<br />
			<br />
			<input type="button" value="Send parameters using POST"
				onclick="doRequestUsingPOST();" />
			<!--调用POST方法主函数-->
		</form>
		<div id="serverResponse"></div>
	</body>
</html>